/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mate.cloud.redisson.controller;

import com.mate.cloud.common.response.BaseResponse;
import com.mate.cloud.common.web.controller.AdminBaseController;
import com.mate.cloud.redisson.dto.TagDTO;
import com.mate.cloud.redisson.service.TagQueryService;
import com.mate.cloud.redisson.service.TagService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * RESTful API 示例
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2025/8/19 10:15
 * @updateUser:     MI
 * @updateTime:     2025/8/19 10:15
 * @updateRemark:   修改内容
 * @version:        1.0
 */
@RestController
@RequestMapping("/api/tag")
public class TagController extends AdminBaseController {

    @Resource
    private TagService tagService;

    @Resource
    private TagQueryService tagQueryService;


    @GetMapping("/index")
    public String addTag(){
        return "/index.html";
    }
    // 为商品添加标签
    @PostMapping("/{itemId}/{tagName}")
    public BaseResponse addTag(
            @PathVariable String itemId,
            @PathVariable String tagName) {
        tagService.addTagToItem(itemId, tagName);
        return successCodeResponse();
    }

    // 移除商品的标签
    @DeleteMapping("/{itemId}/{tagName}")
    public BaseResponse<Void> removeTag(
            @PathVariable String itemId,
            @PathVariable String tagName) {
        tagService.removeTagFromItem(itemId, tagName);
        return successCodeResponse();
    }

    // 获取商品的所有标签
    @GetMapping("/item/{itemId}")
    public BaseResponse<Set<String>> getItemTags(
            @PathVariable String itemId) {
        return successBodyResponse(tagQueryService.getItemTags(itemId));
    }

    // 获取具有指定标签的商品
    @GetMapping("/{tagName}/items")
    public BaseResponse<Set<String>> getItemsByTag(
            @PathVariable String tagName) {
        return successBodyResponse(tagQueryService.getItemsByTag(tagName));
    }

    // 获取热门标签
    @GetMapping("/hot")
    public BaseResponse<List<TagDTO>> getHotTags(
            @RequestParam(defaultValue = "10") int top) {

        Set<ZSetOperations.TypedTuple<String>> hotTags = tagQueryService.getHotTags(top);

        List<TagDTO> result = hotTags.stream()
                .map(tuple -> new TagDTO(tuple.getValue(), tuple.getScore()))
                .collect(Collectors.toList());

        return successBodyResponse(result);
    }
}
